using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Lidgren.Network;
using TiledLib;
using MvdDonk.Bomberman.Managers;
using MvdDonk.Bomberman.Entities;
using MvdDonk.Bomberman.HUD;
using MvdDonk.Bomberman.Screens;


namespace MvdDonk.Bomberman
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Bomberman : Microsoft.Xna.Framework.Game
    {
#if DEBUG
        internal bool DebugEnabled = true;
#else
        internal bool DebugEnabled = false;
#endif

        public static Bomberman Instance;
        private GraphicsDeviceManager graphics;
        private SpriteBatch spriteBatch;


        public Texture2D TestPlayerTexture
        {
            get
            {
                return ResourceManager.RequestTexture("UZ_CharaMap");
            }
        }

        public Texture2D TestBombTexture
        {
            get
            {
                return ResourceManager.RequestTexture("Bomb");
            }
        }

        public Texture2D DevTexture
        {
            get
            {
                return ResourceManager.RequestTexture("Dev");
            }
        }

        #region Managers
        internal InputManager InputManager { get; set; }
        internal GameStateManager GameStateManager { get; set; }
        internal ResourceManager ResourceManager { get; set; }
        internal EntityManager EntityManager { get; set; }
        internal NetworkManager NetworkManager { get; set; }
        internal ScreenManager ScreenManager { get; set; }
        #endregion

        internal DebugBox DebugBox { get; set; }

        List<Entity> players;

        public Bomberman()
        {
            Instance = this;
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            InputManager = new InputManager();
            GameStateManager = new GameStateManager();
            ResourceManager = new ResourceManager();
            EntityManager = new EntityManager();
            NetworkManager = new NetworkManager();
            ScreenManager = new ScreenManager();

            DebugBox = new DebugBox();

            players = new List<Entity>();
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();

            // TODO: Add your initialization logic here
            players = new List<Entity>();
            //BombManager.Initialize();
            DebugBox.Initialize();
            EntityManager.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            ScreenManager.LoadContent();
            ScreenManager.AddScreen(new MenuBackgroundScreen());
            ScreenManager.AddScreen(new MainMenuScreen());
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
            Content.Unload();
        }

        private Vector2 oldMovementVector;

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            ScreenManager.Update(gameTime);
            //GameStateManager.Update(gameTime);
            NetworkManager.Update(gameTime);
            //EntityManager.Update(gameTime);

            DebugBox.Update(gameTime);

            //if (server != null)
            //    server.Poll();

            //BombManager.Update(gameTime);

            //foreach (var player in players)
            //    player.Update(gameTime, map);

            //if (client != null)
            //{
            //    client.Poll();
            //    if (localPlayerRef.MovementVector != oldMovementVector)
            //        client.UpdateMovement(localPlayerRef.MovementState, localPlayerRef.MovementVector);

            //    positionSyncTimer = positionSyncTimer.Add(gameTime.ElapsedGameTime);
            //    if (positionSyncTimer.Milliseconds >= positionSyncInterval)
            //    {
            //        positionSyncTimer = new TimeSpan();
            //        client.SyncPosition(localPlayerRef.Position);
            //    }
            //}




            InputManager.Update();
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            // TODO: Add your drawing code here

            //spriteBatch.Begin();
            //BombManager.Draw(spriteBatch);
            //foreach (var player in players)
            //    player.Draw(spriteBatch);

            ////DebugBox.Draw(spriteBatch);
            //spriteBatch.End();


            //GameStateManager.Draw();
            ScreenManager.Draw(gameTime, spriteBatch);
            base.Draw(gameTime); 
        }
    }
}
